/**
 * filter过滤器功能逻辑 create by zhuchentong
 * 
 * @20170424
 */
var storage = require('util/storage')
var constant = require('config/constant')
var linq = require('libs/linq')
var common = require('util/common')
var dict = require('module/dictModule')
var city = require('module/cityModule')
var log = require('util/log')
/**
 * 日期数据转换 转为-刚刚，今天，昨天 根据constant.common.dateRange进行转换
 */
module.exports.dateConvert = function (date) {
	if (!date && typeof date !== 'string') {
		return date
	}
	data = date.replace(/\-/g, '/');
	date = Date.parse(data)
	
	var now = new Date()

	// 时间戳表示为 今天经过的时间 + n * 每天的时间
	var now_day = Math.floor((now - (now.getTimezoneOffset()*60*1000))/(24*60*60*1000))
	var now_hours = (now - (now.getTimezoneOffset()*60*1000))%(60*60*1000*24)
	var target_day = Math.floor((date - (now.getTimezoneOffset()*60*1000))/(24*60*60*1000))
	var target_hours = (date - (now.getTimezoneOffset()*60*1000))%(60*60*1000*24)
	var dateRange = constant.common.dateRange
	
	if(now_day == target_day){
		var date_value = now_hours - target_hours
		var value = dateRange.find(function (x) {
			return x.min <= date_value && date_value < x.max
		})
	}else{
		var date_value = (now_day - target_day)*(24*60*60*1000)
		var value = dateRange.find(function (x) {
			return x.min <= date_value && date_value < x.max
		})
	}
	
	if (value) {
		return value.text
	} else {
		return common.dateFormat(date, 'yyyy年MM月dd日')
	}
}

/**
 * 对手机号码进行脱敏显示
 */
module.exports.encryptPhone = function (value) {
	if (!value || value === '') {
		return ''
	}

	if (typeof value === 'string' && value.length === 11) {
		return value.substr(0, 3) + '****' + value.substr(7)
	}

	return value
}

/**
 * 对身份证号码进行脱敏显示
 */
module.exports.encryptIdCard = function (value) {
	if (!value || value === '') {
		return ''
	}

	switch (value.length) {
		case 18:
			return value.substr(0, 4) + '**********' + value.substr(14)
		case 15:
			return value.substr(0, 4) + '*******' + value.substr(11)
		default:
			return ""
	}
}


module.exports.toThousands = function (num) {
	if (isNaN(num) || num == "" || num == undefined || num == null) {
		return "";
	}
	num = num + "";
	if (/^.*\..*$/.test(num)) {
		var pointIndex = num.lastIndexOf(".");
		var intPart = num.substring(0, pointIndex);
		var pointPart = num.substring(pointIndex + 1, num.length);
		intPart = intPart + "";
		var re = /(-?\d+)(\d{3})/
		while (re.test(intPart)) {
			intPart = intPart.replace(re, "$1,$2")
		}
		num = intPart + "." + pointPart;
	} else {
		num = num + "";
		var re = /(-?\d+)(\d{3})/
		while (re.test(num)) {
			num = num.replace(re, "$1,$2")
		}
	}
	return num;
}

/**
 * 获取对应typecode对应字典数据
 */
module.exports.getTypeCodeData = dict.getTypeCodeData

/**
 * 根据index获取对应typecode对应数据id
 */
module.exports.getTypeCodeIdByIndex = dict.getTypeCodeIdByIndex

/**
 * 将id转换为对应typecode的文字
 */
module.exports.typeCodeConvert = dict.typeCodeConvert

/**
 * 获取对应城市id的名称
 */
module.exports.getCityName = city.getCityName